home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / BCWINDOW.ZIP;1 / WINDOW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  15.5 KB  |  624 lines

  1. #pragma inline
  2. #include <dos.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <windows.h>
  9.  
  10.  
  11. /*         Window routines for turbo c v2.0 .  Designed by Bill Crum.       **
  12. **                                                                          **
  13. **    get_video_mode ()   sets up screen addresses and colors if needed.    **
  14. **    push_window ()      puts window to the screen and creates pointer for **
  15. **                        other routines.                                   **
  16. **    push ()             puts window to screen with window pointer.        **
  17. **    pop ()              removes window from screen restores screen.       **
  18. **    cursor_off()        turns off cursor.                                 **
  19. **    cursor_on()         turns on cursor.                                  **
  20. **    clr_window()        clear_screen for window pointed to.               **  
  21. **    w_printf()          pseudo printf for window pointed to.              **
  22. **    close_wind ()       clears window pointer and returns memory to dos.  **
  23. **    scroll_window ()    moves window screen up or down 1 line.            **
  24. **    save_screen ()      full screen save to buffer.                       **  
  25. **    restore_screen ()   returns buffer saved to screen.                   **
  26. **    explode_frame ()    makes a window that appears to expand from zero.  **
  27. **    unexplode_frame ()  shrinks window to zero size.                      **
  28. **    draw_frame ()       creates the window border.                        **
  29. **    clear_frame ()      erases the window border.                         **
  30. **    f_write ()          direct writes to screen memory.                   **
  31. **    getchx ()           returns hot key values by adding 300 to ascii.    **
  32. **                                                                          **
  33. **    All the routines were written by myself in advanced c class except    **
  34. **    for two which was taken from the book 'turbo c developers library'.   **
  35. **    I am supplying the source in hopes of impressing someone into hiring  **
  36. **    a junior level programmer.                                            */
  37.   
  38.  
  39. extern int
  40.     baseofscreen;
  41.  
  42. extern char
  43.         waitforretrace,
  44.         normal,
  45.         vidhigh,
  46.         reverse,
  47.         black,
  48.         blue,
  49.         green,
  50.         cyan,
  51.         red,
  52.         magenta,
  53.         brown,
  54.         white,
  55.         bwhite,
  56.         background;
  57.  
  58.  
  59. struct WINDOW windo;
  60.  
  61. struct WINDOW *push_window (int x, int y, int width, int len, int type, int border, char *atr,
  62.                             char *title, char winback)
  63. {
  64.     struct WINDOW *pwindow;                 /* structure to passed around  */
  65.  
  66.     if ( ((x + width) > 80) || ((y + len) > 25) )
  67.         return ( 0 );
  68.     pwindow = (struct WINDOW *) malloc (sizeof(struct WINDOW));    
  69.     pwindow->background = malloc ( width * len * 2 );
  70.     pwindow->foreground = malloc ( width * len * 2 );
  71.     pwindow->x = x;
  72.     pwindow->y = y;
  73.     pwindow->width = width;
  74.     pwindow->len = len;
  75.     pwindow->border = border;
  76.     pwindow->type = type;
  77.     pwindow->atr = atr;
  78.     pwindow->title = title;
  79.     pwindow->winback = winback;
  80.     gettext ( x, y, width + x - 1, len + y - 1, pwindow->background);
  81.     switch (type)
  82.     {
  83.         case 1:
  84.                 explode_frame (pwindow);
  85.                 break;
  86.         default:
  87.                 draw_frame (x, y, width, len, border, atr, title, winback);
  88.     }         
  89.     gettext ( x, y, width + x - 1, len + y - 1, pwindow->foreground);
  90.     return (pwindow);
  91.     
  92. }
  93.  
  94.  
  95.  
  96. int getchx (void)       /* returns hot keys by adding 300 to ascii code  */
  97. {
  98.         char Cha;
  99.  
  100.         if (( Cha = getch ()) != 0 )
  101.                 {
  102.                 return ( Cha );
  103.                 }
  104.         else 
  105.                 {
  106.                 return ( getch () + 300 );
  107.                 }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void clr_window ( struct WINDOW *window )
  114. {
  115.     char blanks[80] =
  116.         {"                                                                          "};
  117.     int count;
  118.     
  119.     blanks[window->width-2] = 0;
  120.     for (count = 0; count < window->len - 2 ; count++)
  121.     {
  122.         f_write ( window->x + 1, window->y + count + 1, window->winback, blanks);
  123.     }           
  124.     gettext ( window->x, window->y, window->width + window->x - 1, window->len + window->y - 1, window->foreground);
  125. }    
  126.  
  127.  
  128.     
  129.  
  130. void w_printf (struct WINDOW *window, int x, int y, char *str, char atr)
  131. {
  132.     if (!window)
  133.         return;
  134.         
  135.     if (( x >= (window->width - 2)) || ( y >= (window->len - 2)))
  136.     return;
  137.  
  138.     if ( strlen (str) > (window->width - 2 - x))
  139.         *(str + (window->width - x ) - 2) = 0;
  140.         
  141.     f_write ( window->x + x + 1, window->y + y + 1, window->winback | (atr >> 4) , str);
  142.     gettext ( window->x, window->y, window->width + window->x - 1, window->len + window->y - 1, window->foreground);
  143.  
  144. }
  145.  
  146. /*  push () puts previous define window to screen  */
  147.  
  148. void push (struct WINDOW *window)
  149. {
  150.     if ( window )
  151.     {
  152.         gettext ( window->x,window->y, window->width + window->x - 1, window->len + window->y - 1, window->background);
  153.         switch (window->type)
  154.         {
  155.             case 1:
  156.                     explode_frame (window);
  157.                     break;
  158.         }         
  159.         puttext ( window->x,window->y, window->width + window->x - 1, window->len + window->y - 1, window->foreground);
  160.     }
  161. }
  162.  
  163.  
  164. /* pop() removes previously defined window from screen  */
  165.  
  166. void pop (struct WINDOW *window)
  167. {
  168.     if ( window )
  169.     {
  170.         switch (window->type)
  171.         {
  172.             case 1: 
  173.                     unexplode_frame (window);
  174.                     break;
  175.             default:
  176.                     puttext ( window->x,window->y, window->width + window->x - 1, window->len + window->y - 1, window->background);
  177.         }
  178.     }
  179. }
  180.  
  181. /* close_wind () nulls defined window and returns memory to dos. */
  182.  
  183. void close_wind (struct WINDOW *window)
  184. {
  185.     if (window)
  186.     {
  187.         pop (window);        
  188.         free ( window->background );
  189.         free ( window->foreground );
  190.         free ( window );
  191.         window = 0;
  192.     }
  193. }        
  194.  
  195.  
  196.  
  197. scroll_window (struct WINDOW *window, char up_down)
  198. {
  199.     union REGS regs;
  200.     
  201.     setmem (®s, sizeof (regs),0);
  202.  
  203.     regs.h.bh = window->winback;
  204.     regs.h.ch = window->y; 
  205.     regs.h.cl = window->x;
  206.     regs.h.dh = window->y + window->len - 3;
  207.     regs.h.dl = window->x + window->width - 3;
  208.     regs.h.al = 1;
  209.     if ( up_down )
  210.     {
  211.         regs.h.ah = 6;
  212.     }
  213.     else
  214.     {
  215.         regs.h.ah = 7;
  216.     }
  217.     int86 (0x10, ®s, ®s);
  218. }    
  219.         
  220.  
  221.  
  222. int oldcursor;
  223.  
  224. void cursor_off ()
  225. {
  226.     union REGS regs;
  227.     
  228.     setmem (®s, sizeof (regs), 0);
  229.     
  230.     regs.h.ah = 0x03;
  231.     int86 (0x10, ®s, ®s);
  232.     oldcursor = regs.x.cx;    
  233.     setmem (®s, sizeof (regs), 0);
  234.     regs.h.ah = 0x01;
  235.     regs.h.ch = 0x20;
  236.     int86 (0x10, ®s, ®s);
  237. }
  238.  
  239.  
  240. void cursor_on ()
  241. {
  242.     union REGS regs;
  243.     
  244.     setmem (®s, sizeof (regs), 0);
  245.     
  246.     regs.h.ah = 0x01;
  247.     regs.x.cx = oldcursor;    
  248.     int86 (0x10, ®s, ®s);
  249. }
  250.  
  251.  
  252. char *screen_buffer;
  253.  
  254.  
  255. save_screen ()
  256. {
  257.     screen_buffer = malloc (4000);
  258.     gettext ( 0,0,79,24, screen_buffer);
  259. }
  260.  
  261.  
  262. restore_screen ()
  263. {
  264.     puttext ( 0,0,79,24, screen_buffer );
  265.     free (screen_buffer);
  266. }
  267.  
  268.  
  269.  
  270. explode_frame (struct WINDOW *window)
  271. {
  272.     int x, y, w, l;
  273.    
  274.     x = (window->x + (window->width / 2));
  275.     y = (window->y + (window->len / 2));
  276.     w = l = 1;
  277.     while ( (w < window->width)||(l < window->len))
  278.     {
  279.         x -= 3;
  280.         if ( x < window->x )
  281.             x = window->x;
  282.         y--;
  283.         if ( y < window->y )
  284.             y = window->y;
  285.         w += 6;
  286.         if ( w > window->width)
  287.             w = window->width;
  288.         l += 2;
  289.         if ( l > window->len )
  290.             l = window->len;
  291.         draw_frame ( x, y, w, l, window->border, window->atr, window->title, window->winback );
  292.     }
  293. }        
  294.            
  295.  
  296. unexplode_frame (struct WINDOW *window)
  297. {
  298.     int x, y, w, l, a, b;
  299.    
  300.     a = b = 0;
  301.     x = window->x;
  302.     y = window->y;
  303.     w = window->width;
  304.     l = window->len;
  305.     while ((w > a) && (l > b))
  306.     {
  307.         delay (15);
  308.         puttext ( window->x,window->y, window->width + window->x - 1, window->len + window->y - 1, window->background);
  309.         draw_frame ( x += 3, y++, w -= 6, l -= 2, window->border,window->atr, window->title, window->winback);
  310.         a += 3;
  311.         b++;
  312.     }
  313.     puttext ( window->x,window->y, window->width + window->x - 1, window->len + window->y - 1, window->background);
  314. }        
  315.  
  316.  
  317. /* original version of draw_frame taken from Turbo C developers library book */
  318.  
  319. void draw_frame (int tlcx, int tlcy, int wide, int len, int lines,
  320.                             char *atr, char *title, char winback)
  321. {
  322.     char 
  323.         batr[2],
  324.         box[6][7] = {"±±±±±±",
  325.                       "⁄¿≥ƒøŸ",
  326.                       "…»∫Õªº",
  327.                       "÷”∫ƒ∑Ω",
  328.                       "’‘≥Õ∏æ",
  329.                       "******"},
  330.         hline[100],
  331.         tline[100];
  332.  
  333.     int c, rx;
  334.  
  335.  
  336.     wide -= 2;
  337.     len -=2;
  338.  
  339.        
  340.     if ((len > 0) && (wide > 0))
  341.         clear_frame (winback, tlcx + 1, tlcy + 1, wide, len);
  342.  
  343.     hline[0] = box[lines][0];
  344.     for ( c = 1; c <= wide; hline[c] = box[lines][3], c++ );
  345.     
  346.     hline[c++] = box[lines][4];
  347.     hline[c] = 0;
  348.     for ( c = 0; c <= 1; c++ )
  349.     {   
  350.         switch (atr[c])
  351.         {
  352.             case 'N': case 'n': batr[c] = normal; break;
  353.             case 'R': case 'r': batr[c] = reverse; break;
  354.             case 'H': case 'h': batr[c] = vidhigh; break;
  355.         }
  356.         if ((waitforretrace) && (!c))
  357.             batr[c] = (batr[c] & 0x0f) | winback;
  358.     }
  359.     f_write (tlcx, tlcy, batr[0], hline);
  360.     hline[0] = box[lines][1];
  361.     hline[wide + 1] = box[lines][5];
  362.     f_write (tlcx, tlcy + len + 1, batr[0], hline);
  363.     
  364.     len += tlcy;
  365.     rx = tlcx + wide + 1;
  366.     hline[0] = box[lines][2];
  367.     hline[1] = 0;
  368.     for (c = tlcy + 1; c <= len; c++)
  369.     {
  370.         f_write (tlcx, c, batr[0], hline);
  371.         f_write (rx, c, batr[0], hline);
  372.     }
  373.     strcpy (tline, title);
  374.     erase_white_end(tline);
  375.     erase_white_begin(tline);
  376.     if (strlen (tline) > 0)
  377.     {
  378.         cen (tline, hline, strlen(tline) + 2);
  379.         rx = (((wide + 2) - strlen(hline) >> 1) + tlcx);
  380.         f_write (rx, tlcy, batr[1], hline);
  381.     }
  382.  
  383. }
  384.  
  385. void clear_frame (char wincolor, int xtl, int ytl, int winwid, int windep)
  386. {
  387.     char clearstring [100];
  388.     int c;
  389.     
  390.     setmem (clearstring, winwid, 32);
  391.     clearstring[winwid] = 0;
  392.     ytl--;
  393.     for (c = 1; c <= windep; c++)
  394.         f_write(xtl, ytl + c, wincolor, clearstring);
  395.     background = wincolor;
  396. }
  397.  
  398.  
  399. char *erase_white_end ( char *st)
  400. {
  401.     int c;
  402.     
  403.     if (st[0])
  404.     {
  405.         for ( c = 0; st[c]; c++ );
  406.         for ( c--; ((c > 0) && ((unsigned) st[c] <= 32)); c-- );
  407.         if ((unsigned) st[c] > 32)
  408.             st[c + 1] = 0;
  409.         else
  410.             st[c] = 0;
  411.     }
  412.     return (st);
  413. }    
  414.  
  415. char *erase_white_begin (char *st)
  416. {
  417.     int c, c1;
  418.     
  419.     c1 = 0;
  420.     for (c= 0; (((unsigned)st[c] <= 32) && (st[c])); c++);
  421.     if (st[c])
  422.     {
  423.         while (st[c])
  424.         {
  425.             st[c1] = st[c];
  426.             c1++;
  427.             c++;
  428.         }
  429.     }
  430.     st[c1] = 0;
  431.     return (st);
  432. }
  433.  
  434. char *cen(char *cstr, char *tstr, int fw)
  435. {
  436.     int t, t1, ls, lc, rc;
  437.     
  438.     ls = strlen (cstr);
  439.     if (ls <= fw)
  440.     {
  441.         lc = (t1 = fw - ls) >> 1;
  442.         rc = lc + (t1 & 0x0001);
  443.         for ( t = 0, lc--; t <= lc; t++)
  444.             tstr[t] = ' ';
  445.         for ( t1 = 0, ls--; t1 <= ls; t++, t1++ )
  446.             tstr[t] = cstr[t1];
  447.         for (rc += t; t < rc ; t++)
  448.             tstr[t] = ' ';
  449.         tstr[t] = 0;
  450.     }
  451.     else
  452.     {
  453.         for ( t = 0, fw--; t <= fw; t++ )
  454.             tstr[t] = cstr[t];
  455.         tstr[t] = 0;
  456.     }
  457.     return (tstr);
  458. }                       
  459.  
  460.  
  461. void get_video_mode()
  462. {
  463. /*     struct vidcfgtype
  464.         {
  465.             char
  466.                 normal,
  467.                 vidhigh,
  468.                 reverse,
  469.                 black,
  470.                 blue,
  471.                 green,
  472.                 cyan,
  473.                 red,
  474.                 magenta,
  475.                 brown,
  476.                 white,
  477.                 bwhite,
  478.                 background;
  479.         };
  480. struct vidcfgtype
  481.     cfgvid;
  482.  */
  483. union REGS
  484.     procreg;
  485.     
  486. memset(&procreg, 0x00, sizeof (procreg));
  487. procreg.h.ah = 0x0f;
  488. int86(0x10, &procreg, &procreg);
  489.  
  490. switch (procreg.h.al)
  491.     {
  492.     case 0x07:
  493.                 baseofscreen = 0xb000;                        
  494.                 waitforretrace = 0x00;
  495.                 normal = 0x07;
  496.                 vidhigh = 0x0f;
  497.                 reverse = 0x70;
  498.                 black = blue = green = cyan = red = 
  499.                 magenta = brown = white = bwhite = 0x07;
  500.                 background = black;
  501.                 break;
  502.     case 0x00:
  503.     case 0x01:
  504.     case 0x02:
  505.     case 0x03:
  506.     case 0x04:
  507.     case 0x05:
  508.     case 0x06:
  509.                 baseofscreen = 0xb800;
  510.                 waitforretrace = 0x00;
  511.                 normal = 0x17;
  512.                 vidhigh = 0x3f;
  513.                 reverse = 0x57;
  514.                 black = 0x00;
  515.                 blue = 0x10;
  516.                 green = 0x20;
  517.                 cyan = 0x30;
  518.                 red = 0x40;
  519.                 magenta = 0x50;
  520.                 brown = 0x60;
  521.                 white = 0x70;
  522.                 bwhite = 0xf0;
  523.                 background = blue;
  524.                 break;
  525.  
  526.     case 0x08:
  527.     case 0x09:
  528.     case 0x0a:
  529.     case 0x0d:
  530.     case 0x0e:
  531.     case 0x0f:
  532.                 baseofscreen = 0xb800;
  533.                 waitforretrace = 0x01;
  534.                 normal = 0x17;
  535.                 vidhigh = 0x3f;
  536.                 reverse = 0x41;
  537.                 black = 0x00;
  538.                 blue = 0x10;
  539.                 green = 0x20;
  540.                 cyan = 0x30;
  541.                 red = 0x40;
  542.                 magenta = 0x50;
  543.                 brown = 0x60;
  544.                 white = 0x70;
  545.                 bwhite = 0xf0;
  546.                 background = blue;
  547.                 break;
  548.     default:    
  549.                 printf ("Must be in text mode\n");
  550.                 exit (0);
  551.                 
  552.     }/* switch */
  553.  
  554. }              
  555.                     
  556. /* f_write is copied from Turbo C Developers library book  */
  557.  
  558. void f_write (unsigned int x, unsigned int y, char attr, char *st)
  559.  
  560. {
  561.     asm    mov    al,00h
  562.     asm    push    ds
  563.     asm    pop    es
  564.     asm    mov    di, st
  565.     asm    mov    bx, di
  566.     asm    mov    cx, 0ffffh
  567.     asm    cld
  568.     asm    repne    scasb
  569.     asm    sub    di, bx
  570.     asm    mov    bx, di
  571.     asm    dec    bx
  572.     asm    mov    si, st
  573.     asm    mov    ax, y
  574.     asm    dec    ax
  575.     asm    mov    cx, 0004h
  576.     asm    shl    ax,cl
  577.     asm    mov    cx,bx
  578.         asm     mov     bx,ax
  579.     asm    shl    ax, 1
  580.     asm    shl    ax, 1
  581.     asm     add    ax,bx
  582.     asm    mov    bx,x
  583.     asm    dec    bx
  584.     asm    add    ax,bx
  585.     asm    shl    ax,1
  586.     asm    mov    di,ax
  587.     asm    mov    dx,baseofscreen
  588.     asm    mov    es,dx
  589.     asm    mov    al,waitforretrace
  590.     asm     mov    si, st
  591.     asm    jcxz    Exit
  592.     asm    mov    ah,attr
  593.     asm    cld
  594.     asm    rcr    al,1
  595.     asm    jnc    Mono
  596.     asm    mov    dx, 03dah
  597. GetNext:
  598.     asm     lodsb
  599.     asm    mov    bx,ax
  600.     asm    mov    ah, 09h
  601.     asm    cli
  602. Waith:
  603.     asm    in    al,dx
  604.     asm    rcr    al, 1
  605.     asm    jc    Waith
  606. Waitv:
  607.     asm    in    al,dx
  608.     asm    and    al,ah
  609.     asm    jz    Waitv
  610.     asm    mov    ax,bx
  611.     asm    stosw
  612.     asm    sti
  613.     asm    loop    GetNext
  614.     asm    jmp    Exit
  615. Mono:
  616.     asm    lodsb
  617.     asm    stosw
  618.     asm    loop    Mono
  619. Exit:
  620. /*      asm     pop    ds */
  621. ;
  622. }
  623.  
  624.